package com.yonyou.ucf.mdf.app.extend.event;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Sets;
import com.yonyou.iuap.context.InvocationInfoProxy;
import com.yonyou.iuap.event.model.BusinessEvent;
import com.yonyou.iuap.yms.multitenant.UserExecutors;
import com.yonyou.yonbip.iuap.xport.importing.event.EventReceiveResult;
import com.yonyou.yonbip.iuap.xport.importing.event.ImportEventConsumerImpl;
import com.yonyou.ypd.bill.infrastructure.service.api.IBillContextService;
import com.yonyou.ypd.mdf.adapter.utils.GsonExtUtils;
import com.yonyoucloud.iuap.ucf.mdd.starter.ucg.openapi.module.isv.event.ISVEventListener;
import com.yonyoucloud.iuap.ucf.mdd.starter.ucg.openapi.network.cryptor.EncryptionHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * @Author: zhuyjh
 * @Date: 2022/10/18 16:06
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ISVEventBatchImportEvent implements ISVEventListener<ISVDemoEventClass> {

    @Nullable
    private final ImportEventConsumerImpl eventConsumer;

    @Autowired
    private IBillContextService billContextService;

    @Override
    public boolean onEvent(String type, ISVDemoEventClass event, EncryptionHolder holder) {
        //已经解密过了
        String content = event.getContent();
        if (StringUtils.isNotBlank(content)){
            log.info(">>>#onEvent#ISVEventBatchImportEvent:{}", content);
            if (content != null && content.length() != 0) {
                BusinessEvent businessEvent = (BusinessEvent) JSONObject.parseObject(content, BusinessEvent.class);
                businessEvent.setUserObject(content);
                Map<String,Object> reveiveData = GsonExtUtils.getMap(content);
                String yhtAccessToken = reveiveData.get("yht_access_token").toString();
                UserExecutors.executeAsInitialThread(yhtAccessToken, () -> {
                    log.info("上下文中的 租户id:{}, 用户id:{}", InvocationInfoProxy.getTenantid(), InvocationInfoProxy.getUserid());
                    batchImport(businessEvent);
                    return null;
                });
            }
            return true;
        }
        return false;
    }
    public EventReceiveResult batchImport(BusinessEvent businessEvent) {
        if (businessEvent == null) {
            log.info(">>>#EventBatchImport#参数信息为空，不需要进行导入！");
            return null;
        }

        if (eventConsumer == null) {
            return EventReceiveResult.failed("consumer not init");
        }
        return eventConsumer.processImportEvent(businessEvent);
    }

    @Override
    public int priority() {
        return ISVEventListener.super.priority()-145;
    }

    @Override
    public Set<String> supportTypes() {
        return Sets.newHashSet(ISVDemoEventExtendType.EVENT_BATCH_IMPORT);
    }

    @Override
    public Class<ISVDemoEventClass> getEventClass() {
        return ISVDemoEventClass.class;
    }
}
